﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CodeEditor
{
    public partial class MethodWindow : DigitalRune.Windows.Docking.DockableForm
    {
        //Property zum Setzen der geschützten Eigenschaft "editorBox.Text"

        public class MethodSettings : INotifyPropertyChanged
        {
            public MethodSettings()
            {
                Parameters = new List<Parameter>();
                MethodName = "";
                ReturnType = "System.Void";
            }

            public void NotifyPropertyChanged(String property)
            {
                if(PropertyChanged != null){
                    PropertyChanged(this, new PropertyChangedEventArgs(property));
                }
            }

            public struct Parameter
            {
                [System.ComponentModel.DisplayName("Parameter Name"),
                System.ComponentModel.CategoryAttribute("Parameter")]
                public String ParameterName { get; set; }
                [System.ComponentModel.DisplayName("Parameter Type"),
                System.ComponentModel.CategoryAttribute("Parameter"),
                TypeConverter(typeof(TypeListConverter))]
                public String ParameterType { get; set; }
            }

            [System.ComponentModel.CategoryAttribute("Method Settings"), System.ComponentModel.DefaultValueAttribute("System.Void"),
            System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.Repaint),
            TypeConverter(typeof(TypeListConverter))]
            public String ReturnType
            {
                get;
                set;
            }
            [System.ComponentModel.CategoryAttribute("Method Settings"), System.ComponentModel.ReadOnly(true)]
            public String MethodName
            {
                get;
                set;
            }
            [System.ComponentModel.CategoryAttribute("Method Settings")]
            public List<Parameter> Parameters
            {
                get;
                set;
            }

            public event PropertyChangedEventHandler PropertyChanged;
        }

        public MethodSettings Settings
        {
            get;
            set;
        }

        public String EditorText
        {
            get
            {
                return editorBox.Text;
            }
            set
            {
                editorBox.Text = value;
            }
        }


        public QWhale.Editor.SyntaxEdit EditorBox
        {
            get
            {
                return editorBox;
            }
        }

        public MethodWindow(bool isMainMethod, bool isEnabled)
        {
            InitializeComponent();
            
            Settings = new MethodWindow.MethodSettings();
            Settings.PropertyChanged += new PropertyChangedEventHandler(Settings_PropertyChanged);
            propertyGridMethodProperties.SelectedObject = Settings;
            QWhale.Syntax.CsMethodBodyParser parser = new QWhale.Syntax.CsMethodBodyParser("MyNamespace","MyClass","Run","");
            parser.RegisterNamespace("System");
            parser.RegisterNamespace("System.Drawing");
            parser.SyntaxTree.Current.Position = new Point(-1, -1);
            editorBox.Lexer = parser;
            
            if (isMainMethod)
            {
                propertyGridMethodProperties.Enabled = false;
            }
            this.Enabled = isEnabled;
        }

        void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            propertyGridMethodProperties.Refresh();
        }

    }
    
    class TypeListConverter : StringConverter
    {
        List<String> Types
        {
            get;
            set;
        }

        public TypeListConverter()
        {
            Types = new List<String>();
            foreach (System.Reflection.Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (Type t in asm.GetTypes())
                {
                    if(!Types.Contains(t.ToString()) && t.Namespace == "System")
                    {
                        Types.Add(t.ToString());
                    }
                }
            }
        }

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new TypeConverter.StandardValuesCollection(Types);
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return false;
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (value as String == String.Empty)
            {
                return "System.Void";
            }
            else
            {
                return value;
            }
        }
    }
}
